home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / lspsql.zip / ASITEST.LSP < prev    next >
Lisp/Scheme  |  1992-08-25  |  14KB  |  467 lines

  1. ;;;---------------------------------------------------------------------------
  2. ;;;
  3. ;;;   asitest.lsp
  4. ;;;   Copyright (C) 1991-1992 by Autodesk, Inc.
  5. ;;;      
  6. ;;;   Permission to use, copy, modify, and distribute this software 
  7. ;;;   for any purpose and without fee is hereby granted, provided 
  8. ;;;   that the above copyright notice appears in all copies and that 
  9. ;;;   both that copyright notice and this permission notice appear in 
  10. ;;;   all supporting documentation.
  11. ;;;
  12. ;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
  13. ;;;   WARRANTY.  ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR
  14. ;;;   PURPOSE AND OF MERCHANTABILITY ARE HEREBY DISCLAIMED.
  15. ;;;
  16. ;;;   by Frumkin A.
  17. ;;;   April 20 1992
  18. ;;;
  19. ;;;--------------------------------------------------------------------------
  20. ;;;  DESCRIPTION
  21. ;;;
  22. ;;;  Test ASI. Allows customers to enter SQL statements and edit database
  23. ;;;  tables.
  24. ;;;
  25. ;;;----------------------------------------------------------------------------
  26.  
  27. ;;;----------------------------------------------------------------------------
  28. ;;; Defined c: so that it can be used at the Command Line..
  29. ;;;----------------------------------------------------------------------------
  30.   (defun c:sqldrv()
  31.     (sqldrv)
  32.   )
  33.   (defun c:sqlcnc()
  34.     (sqlcnc)
  35.   )
  36.   (defun c:sqlterm()
  37.     (sqlterm)
  38.   )
  39.   (defun c:sql()
  40.     (sql)
  41.   )
  42.   (defun c:testbind()
  43.     (testbind)
  44.   )
  45.   (defun c:sqlfile()
  46.     (sqlfile)
  47.   )
  48.   (defun c:sqldis()
  49.     (sqldis)
  50.   )
  51.   
  52.   ;;
  53.   ;; Compilation of SQL statement.
  54.   ;;
  55.   (defun compile (hcon stm /
  56.                   hcom
  57.                   )
  58.     (if (setq hcom (asi_ohdl hcon))
  59.        (progn
  60.            (if (and (asi_com hcom stm) (asi_exe hcom))
  61.                 (if (eq (asi_stm hcom) "ASI_CURSOR") 
  62.               (scan hcom)
  63.                  (princ "\nOK\n")
  64.                  )
  65.            )
  66.             (asi_chdl hcom)
  67.        )
  68.     )
  69.   )
  70.  
  71.   ;;
  72.   ;; Fetching table.
  73.   ;;
  74.   (defun scan (hcom / flag com prev prompt)
  75.      (setq prev "Exit")
  76.      (while (not flag)
  77.          (print_row hcom)
  78.          (setq prompt
  79.           (strcat "\nFirst/Last/Next/Previous/Delete/Update/Show/Exit/<"
  80.               prev ">: "))
  81.       (initget 0 "First Last Next Previous Delete Update Show Exit")
  82.       (setq com (getkword prompt))
  83.       (if (= com nil)(setq com prev))
  84.       (cond 
  85.           ((eq com "First")
  86.               (progn 
  87.                  (princ "\nTop")
  88.                  (asi_ftr hcom)
  89.               )
  90.           )
  91.           ((eq com "Last")  
  92.               (progn 
  93.                  (princ "\nBottom")
  94.                  (asi_fbr hcom)
  95.               )
  96.           )
  97.           ((eq com "Next")        (asi_fet hcom))
  98.           ((eq com "Previous")   (asi_fbk hcom)) 
  99.           ((eq com "Delete") 
  100.               (if (asi_del hcom)     (princ "\nCurrent line deleted"))
  101.           )
  102.           ((eq com "Update")         (update_row hcom))
  103.           ((eq com "Show")           (print_set hcom))
  104.           ((eq com "Exit")           (setq flag T))
  105.       )
  106.       (if (not (= com nil)) (setq prev com))
  107.      )
  108.   )
  109.  
  110.   ;;
  111.   ;; Prints row from table
  112.   ;;
  113.   (defun print_row (hcom)
  114.      (print_header hcom)
  115.      (if (= (fix (asi_currow hcom)) -2)
  116.          (princ "\nEOS")
  117.           (if (= (fix (asi_currow hcom)) -1) 
  118.              (princ "\nTOS")
  119.            (print_data hcom)
  120.           )
  121.      )     
  122.   )
  123.   
  124.   ;;
  125.   ;; Print data from table.
  126.   ;;
  127.   (defun print_set (hcom / rows flag)
  128.      (print_header hcom)
  129.      (setq rows 0)
  130.      (asi_ftr hcom)
  131.      (if (= (fix (asi_currow hcom)) -2)
  132.          (princ "\nEOS")
  133.       (if (= (fix (asi_currow hcom)) -1) 
  134.          (princ "\nTOS")
  135.              (while (not flag)
  136.                  (print_data hcom)
  137.               (setq rows (1+ rows))
  138.               (if (null (asi_fet hcom)) (setq flag T))
  139.              )
  140.       )
  141.      )     
  142.      (asi_ftr hcom)
  143.      (princ (strcat "\n" (itoa rows) " rows selected"))
  144.      (getstring "\nPress RETURN...")
  145.   )
  146.   
  147.   ;;
  148.   ;; Print column names.
  149.   ;; 
  150.   (defun print_header (hcom / str jj lst len l)
  151.      (setq str "\n    |" jj  0)
  152.      (while (setq lst (asi_cds hcom jj))
  153.          (setq jj (1+ jj))
  154.          (setq len (strlen (nth 0 lst)))
  155.          (if (< len (nth 1 lst)) (setq l (nth 1 lst)) (setq l len))
  156.          (setq str (strcat str (addlist (nth 0 lst) l) " | "))
  157.      )
  158.      (princ str)
  159.      (princ "\n    |--------------------")
  160.   )
  161.   
  162.   ;;
  163.   ;; Prints table.
  164.   ;;
  165.   (defun print_data (hcom / l lst len val jj tp str)
  166.       (setq str (strcat "\n" (addlist (itoa (+ 1 (fix (asi_currow hcom)))) 4) "|")
  167.             jj 0)
  168.       (while (setq val (asi_cvl hcom jj))
  169.           (setq lst (asi_cds hcom jj)
  170.                 tp (type val)
  171.                 len (strlen (nth 0 lst))
  172.           )
  173.           (if (< len (nth 1 lst)) (setq l (nth 1 lst)) (setq l len))
  174.           (cond 
  175.                ((= tp 'INT)    
  176.                   (setq str 
  177.                       (strcat str (addlist (itoa val) l) " | "))
  178.               )
  179.                ((= tp 'REAL) 
  180.                   (setq str 
  181.                       (strcat str (addlist (rtos val 2 (nth 2 lst)) l) " | "))
  182.               )
  183.               (T (setq str (strcat str (addlist val l) " | ")))
  184.           )
  185.           (setq jj (1+ jj))
  186.       )
  187.       (princ str)
  188.       (terpri)
  189.   )
  190.   
  191.   ;; 
  192.   ;; Adds space for sting while it length leth then defined.
  193.   ;;
  194.   (defun addlist (str len / l)
  195.      (setq l (strlen str)) 
  196.      (while (< l len)
  197.         (setq l (1+ l) str (strcat str " "))
  198.      )
  199.      (setq str str)
  200.   )
  201.   
  202.   ;;
  203.   ;; Updates current row.
  204.   ;;
  205.   (defun update_row (hcom / ii flag cds prompt val newval tp)
  206.      (if (>= (fix (asi_currow hcom)) 0 )
  207.         (progn
  208.            (princ "\n -------Update current row --------------\n")
  209.        (setq ii 0 flag T)
  210.        (while (and flag (setq cds (asi_cds hcom ii)))
  211.             (setq val (asi_cvl hcom ii) 
  212.                 prompt (strcat "\n" (nth 0 cds) "<")
  213.                 tp (type val)
  214.            )
  215.            (cond 
  216.                ((= tp 'INT)    
  217.                   (setq prompt (strcat prompt (itoa val) ">: "))
  218.               )
  219.                ((= tp 'REAL) 
  220.                   (setq prompt (strcat prompt (rtos val 2 (nth 2 cds)) ">: "))
  221.               )
  222.               (T 
  223.                   (setq prompt (strcat prompt val ">: "))
  224.                )
  225.            )
  226.            (setq newval (getstring prompt))
  227.            (if (not (= newval ""))
  228.                   (if (= newval "NULL")  
  229.                      (setq flag (asi_upd hcom (nth 0 cds) ""))
  230.                      (setq flag (asi_upd hcom (nth 0 cds) newval))
  231.                   )) 
  232.            (if (not flag) (princ "  error") (setq ii (1+ ii)))
  233.        )
  234.         )
  235.      )
  236.   )
  237.   
  238.   ;;
  239.   ;; Reads SQL statements from file and exequtes them.
  240.   ;;
  241.   (defun readFile (hcon fname / pt hcom)
  242.      (if (setq hcom (asi_ohdl hcon))
  243.         (if (setq pt (open fname "r"))
  244.            (while (setq str (strRead pt))
  245.               (if (not (= str ""))
  246.                   (if (asi_cex hcom str)
  247.                      (progn
  248.                          (princ "\nOK\n")       
  249.                          (if (eq (asi_stm hcom) "ASI_CURSOR") (scan hcom))
  250.                      ) 
  251.                      (progn
  252.                         (princ "\nError")
  253.                         (asi_errmsg hcom)
  254.                      )
  255.                   )
  256.               )
  257.            )
  258.         )
  259.         (asi_chdl hcom)
  260.      )
  261.   )
  262.  
  263.   ;;
  264.   ;; Reads one SQL statement from the file.
  265.   ;;   
  266.   (defun strRead (pt / flag flag1 str workstr l i j)
  267.      (setq str "" flag T)
  268.      (while flag
  269.         (setq workstr (read-line pt)  flag1 T) 
  270.         (if (not workstr)
  271.            (setq flag nil str nil)
  272.            (progn
  273.               (if (= "$" (substr workstr 1 1))
  274.                   (princ (strcat "\nComment: " (substr workstr 2)))
  275.                   (progn
  276.                      (setq l (strlen workstr) i 1)
  277.                      (while (and (<= i l) (= (substr workstr i 1) " "))
  278.                           (setq i (1+ i))
  279.                      ) 
  280.                      (setq j i)
  281.                      (while (and flag1 (<= j l))
  282.                          (if (= "&" (substr workstr j 1)) 
  283.                              (setq flag1 nil)
  284.                              (setq j (1+ j))
  285.                          )
  286.                      )
  287.                      (if flag1
  288.                          (setq str (strcat str (substr workstr i))
  289.                                flag nil
  290.                          ) 
  291.                          (setq str (strcat str (substr workstr i (- j i))))
  292.                      )
  293.                   )     
  294.               )
  295.            )                  
  296.         )
  297.      )
  298.      (terpri)
  299.      (if str (princ str))
  300.      (setq str str)
  301.   )
  302.   
  303.   ;;
  304.   ;; Error handle.
  305.   ;;
  306.   (defun my_err (s)                  ; If an error (such as CTRL-C) occurs
  307.                                      ; while this command is active...
  308.   
  309.           (if (/= (substr s 1 4) QUIT)
  310.              (princ s)
  311.           )
  312.           (setq *error* older)      ; restore old *error* handler
  313.           (prin1)
  314.   )
  315.   
  316.   (defun sqlcnc ()
  317.       (if hdrv
  318.           (progn
  319.               (setq olderr *error* *error* my_err)  
  320.               (if hcon (asi_lof hcon))
  321.               (setq basename (getstring "\nDatabase name ->"))
  322.               (setq username (getstring "\nUser name ->"))
  323.               (setq password (getstring "\nPassword ->"))
  324.               (if (setq hcon (asi_lon hdrv basename username password))
  325.                   (princ "OK")
  326.                   (princ (strcat "\nCannot connect to database " basename))
  327.               )
  328.               (setq *error* older)      ; restore old *error* handler
  329.           )
  330.           (progn
  331.               (princ "No active drivers detected.")
  332.               (setq hcon nil)
  333.           )
  334.       )
  335.       (prin1)
  336.   )
  337.   
  338.   (defun sqldis ()
  339.       (if hcon 
  340.           (if (asi_lof hcon) (setq hcon nil))
  341.           (princ "No active data base detected.")
  342.       )
  343.       (prin1)
  344.   )
  345.   
  346.   (defun sql ( / statement prompt)
  347.       (if hcon
  348.           (progn
  349.               (setq olderr *error* *error* my_err)  
  350.               (setq prompt 
  351.     (strcat "\nEnter SQL statement.\n" drvname "\\" basename "\\" username ">")
  352.               )
  353.               (while (not (eq (setq statement (getstring T prompt)) ""))
  354.                   (compile hcon statement)
  355.               )
  356.               (setq *error* older)      ; restore old *error* handler
  357.           )
  358.           (princ "No active tables detected.")
  359.       )
  360.       (prin1)
  361.   )
  362.   
  363.   (defun sqlterm ()
  364.       (if hdrv (if (asi_termdrv  hdrv) (setq hdrv nil hcon nil)))
  365.       (prin1)
  366.   )
  367.   
  368.   (defun testbind ( / com hcom val name htype length)
  369.       (if hcon
  370.           (progn
  371.           (setq olderr *error* *error* my_err)  
  372.           (while (not (eq "" (setq com (getstring T "\nSQL STATEMENT>"))))
  373.               (if (and (setq hcom (asi_ohdl hcon)) (asi_com hcom com))
  374.                   (progn
  375.                         (while (not (= "" (setq name 
  376.                               (getstring "\nHost variable name: "))))
  377.                           (initget "Char Int Real Short Long Float")
  378.                           (setq htype (strcat "ASI_H" 
  379.           (getkword "\nVariable type Char/Int/Real/Short/Long/Float: ")))
  380.                           (setq val (getstring T "\nEnter host variable value: "))
  381.                           (initget 1)
  382.                           (setq length (getint "Length: "))    
  383.                           (if (asi_bnd hcom name val htype length)
  384.                                 (princ "\nOK\n")
  385.                                 (princ (strcat "\nBind Error: "
  386.                                    (asi_errmsg hcom) "\n"))
  387.                           )
  388.                       )
  389.                       (if (asi_exe hcom)
  390.                           (progn 
  391.                               (princ "\nOK\n")       
  392.                                 (if (eq (asi_stm hcom) "ASI_CURSOR") (scan hcom))
  393.                           ) 
  394.                           (progn
  395.                               (princ "\nError")
  396.                               (asi_errmsg hcom)
  397.                           )
  398.                       )
  399.                       (asi_chdl hcom)
  400.                   )
  401.               )
  402.           )
  403.           (setq *error* older)      ; restore old *error* handler
  404.           )
  405.       (princ "\nNo active data base")
  406.       )
  407.       (prin1)
  408.   )
  409.   
  410.   (defun sqlfile ( / fname)
  411.       (if hcon
  412.           (progn
  413.               (setq olderr *error* *error* my_err)  
  414.               (if (not (= "" (setq fname (getstring "Enter file name: "))))
  415.                   (if (setq fname (findfile fname))
  416.                       (readFile hcon fname)
  417.                       (princ "\nBad file name")
  418.                   )
  419.               )
  420.               (setq *error* older)      ; restore old *error* handler
  421.           )
  422.           (princ "No active tables detected.")
  423.       )
  424.       (prin1)
  425.   )
  426.   
  427.   (defun sqldrv ()
  428.       (if asi_initdrv
  429.           (progn    
  430.               (setq olderr *error* *error* my_err)
  431.               (if hdrv (asi_termdrv hdrv))
  432.               (setq hcon nil)
  433.               (initget 1)
  434.               (setq drvname (getstring "\nEnter SQL driver name: "))
  435.               (if (setq hdrv (asi_initdrv drvname))
  436.                   (princ "\nDrive loaded")
  437.                   (princ (strcat "\nCannot load " drvname))
  438.               )
  439.               (setq *error* older)      ; restore old *error* handler
  440.           )
  441.           (princ "\nLoad 'LISPSQL.EXP' before execution.")    
  442.       )
  443.       (prin1)
  444.   )
  445.  
  446. ;;;--------------------------------------------------------------------------
  447.  
  448. (princ "ASITEST loaded:\n") 
  449. (princ "\nSQLDRV or (SQLDRV)     - Driver Initialization")
  450. (princ "\nSQLCNC or (SQLCNC)     - Open a Handle to a Database")
  451. (princ "\nSQLDIS or (SQLDIS)     - Release the Connection to a Database")
  452. (princ "\nSQLFILE or (SQLFILE)   - Execute SQL Statements from a file")
  453. (princ "\nSQL or (SQL)           - Execute SQL Statements defined in the")
  454. (princ "\n                         dialogue and Fetching the results of")
  455. (princ "\n                         'cursor' commands")
  456. (princ "\nTESTBIND or (TESTBIND) - Execute SQL Statements with Host Variables")
  457. (princ "\nSQLTERM or (SQLTERM)   - Release the Driver.")
  458. (princ)
  459.  
  460.   
  461.   
  462.   
  463.   
  464.   
  465.   
  466.  
  467.